#ifndef _SBPARSE_H_
#define _SBPARSE_H_

#include <stdio.h>
#include <unistd.h>
#include <ares.h>
#include "sbalance.h"
#include "sblist.h"

#define SB_DNS_MAX_IP_LEN		65

#define SB_DNS_STATUS_DESTORYED		-1	/* not in wait or parse list and channel destoryed */
#define SB_DNS_STATUS_INITED		0	/* temp status, not in wait or parse list after inited */
#define SB_DNS_STATUS_WAITING		1	/* int wait list*/
#define SB_DNS_STATUS_PARSING		2	/* in parse list */
#define SB_DNS_STATUS_OK		3	/* parse ok, notice not in wait or parse and channel destoryed */
#define SB_DNS_STATUS_TIMEOUT		4	/* temp status */
#define SB_DNS_STATUS_ERROR		5	/* temp status */

struct sb_dns
{
	char host[SB_DNS_MAX_HOSTNAME_LEN];
	char ip[SB_DNS_MAX_IP_LEN];	/* only save one ip address */
	
	int status;		/* 0:inited 1: parsing 2: done 3: timeout*/
	ares_channel channel;

	unsigned long timeout_time;
	struct list_head list_node;
	struct hlist_node hash_node;

	/* the waiting for dns this request */
	struct list_head waiting_session_list;
};

int sb_dns_init_env();
/****
1. SB_OK(hit host's ip)
2. SB_AGAIN(a. not find host's ip, but add query request, b. find host, but the status is parsing)
3. SB_TIMEOUT(find host, but the status is timeout)   temp status, not possible status 
4. SB_ERROR
****/
int sb_dns_query_host_ip(const char *host, struct sb_session *sess);
int sb_dns_modify_status(char *host, int new_status);
void sb_dns_remove_session(struct sb_session *sess);
int sb_dns_process();
void sb_dns_set_done_handler(struct sb_session *sess, void (*handler)(void*, int, int, struct hostent*) );
int sb_dns_reparse_host(struct sb_dns *dns, struct sb_session *sess);

#endif
